package com.threadlocal.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @ClassName: ConnectionManagerThreadLocal
 * @Description: ThreadLocal管理ConnectionManager
 * @Author: PAUL XUE
 * @Date: 2022/1/13 11:35 AM
 * @Version: 1.0
 **/
public class ConnectionManagerThreadLocal {
    private static String url = "jdbc:mysql://172.16.196.49:3306/posx_prd?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=Hongkong&zeroDateTimeBehavior=convertToNull";
    private static String user = "root";
    private static String pwd = "CeShi#0301!";
    private static final ThreadLocal<Connection> dbConnectionLocal = new ThreadLocal<Connection>() {
        @Override
        protected Connection initialValue() {
            try {
                return DriverManager.getConnection(url, user, pwd);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    };

    public Connection getConnection() {
        return dbConnectionLocal.get();
    }
}
